package org.framework.converter;

import java.math.BigDecimal;

public interface EnumBase<T> {

    T getCode();
    String getMessage();

    static <E extends Enum<?> & EnumBase> E codeOf(Class<E> enumClass, Object code) {
        E[] enumConstants = enumClass.getEnumConstants();
        for (E e : enumConstants) {
            if(e.getCode() instanceof String){
                if(e.getCode().equals(code.toString())){
                    return e;
                }
            }else if(e.getCode() instanceof Integer){
                if(Integer.valueOf(e.getCode().toString()).intValue() == Integer.valueOf(code.toString()).intValue()){
                    return e;
                }
            }else if(e.getCode() instanceof Double){
                if(Double.valueOf(e.getCode().toString()).doubleValue() == Double.valueOf(code.toString()).doubleValue()){
                    return e;
                }
            }else if(e.getCode() instanceof BigDecimal){
               if( e.getCode().toString().equals(code.toString())){
                   return e;
               }
            }else if(e.getCode() instanceof  Class){
                if( ((Class)e.getCode()).getName().equals(((Class)code).getName())){
                    return e;
                }
            }
        }
        throw new IllegalArgumentException("the code didn't match any enum,code:" + code + ",enum class:" + enumClass.getName());
    }
}
